use std::env;

use anyhow::Result;
use dotenv::dotenv;
use ru::trace;
use service::setup_routers;
use tokio::net::TcpListener;
use tower_http::services::{ServeDir, ServeFile};
use tracing::info;

#[tokio::main]
async fn main() -> Result<()> {
    dotenv().ok();

    trace::init(env::var("RUST_LOG")?.parse()?);

    let addr = env::var("SERVER")?;

    let app = setup_routers().await?;

    let app = app.fallback_service(
        ServeDir::new("dist").not_found_service(ServeFile::new("dist/index.html")),
    );

    let listener = TcpListener::bind(addr).await?;
    info!("server listening on {}", listener.local_addr()?);

    axum::serve(listener, app).await?;

    Ok(())
}
